home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DEMOS / OTEFFEX1.ZIP / EFFECT#1.DOC next >
Encoding:
Text File  |  1996-09-20  |  8.1 KB  |  165 lines

  1.  
  2.                      - THE OUTLAW TRIAD DEMO-SERIES -
  3.  
  4. ───────────────────────────────■ PART XI ■────────────────────────────────────
  5.  
  6.                          Written by : Vulture/OT
  7.                          Code in    : Pascal
  8.                          Topics     : 3d glenzing, flatshading
  9.  
  10. ──────────────────────────────■ Introduction ■────────────────────────────────
  11.  
  12.  Welcome to the Outlaw Triad demo-series! In these series we will be talking
  13.  about programming demo-effects in either pascal or assembler. Theory behind
  14.  the effects shall be discussed while a full sourcecode is also provided.
  15.  Right, we will continue our discussions on 3d graphics. This time we'll be
  16.  talking about some relatively simple concepts. 3d glenzing and flatshading!
  17.  These effects are pretty easy to implement if you know your 3d. This doc is
  18.  an add-on to part 10 in which we created a basic 3d engine. Ya really should
  19.  read that text first before reading this. Otherwise you might get a little
  20.  confused. Or maybe not... or maybe you will... naah, enough said... Enjoy!
  21.  
  22. ─────────────────────────────────■ Theory ■───────────────────────────────────
  23.  
  24.  - GLENZING -
  25.  
  26.  First of all, let me explain what glenzing means. To put it simply, imagine
  27.  a cube made out of colored glass. Whenever you look through a side, you see
  28.  the sides lying behind it. But... in a slightly different color than that
  29.  side really has. Why? Obviously, because the color of the side which you are
  30.  looking through effects the color of the sides behind it.
  31.  
  32.  Ok, with that clear, how do we code this stuff? It's real easy! Here we go:
  33.  
  34.  Most important, in the previous tutorial (on face sorting) we sorted all our
  35.  polygons on their average z value. We don't do this when showing a glenzing
  36.  3d object because it isn't necessary! It doesn't matter in which order you
  37.  draw the polygons. Ok, as you probably know, a polygon is drawn by drawing
  38.  each horizontal scanline it is consists of. In the previous tutorial (#10)
  39.  we just used one constant color for each polygon. This time, while drawing,
  40.  we don't just put one color of the polygon on the screen but we'll also use
  41.  the background color of the pixel of the current scanline we are plotting.
  42.  So, the algorithm for drawing one pixel of one scanline looks like:
  43.  
  44.     - grab background color
  45.     - add polygon color
  46.     - plot pixel on the screen
  47.     - point to next vga spot
  48.  
  49.  All you have to do is change a few lines of code in your horizontal line
  50.  routine and you're done. Of course this is quite a bit slower than just
  51.  plotting a row of pixels of one constant color.
  52.  
  53.  Ok, here's a part of the horizontal line routine. Examine it. Shouldn't
  54.  be too hard to understand. Of course you have to calculate the exact vga
  55.  position first before using this code. Check the complete source code for
  56.  the total routine.
  57.  
  58.       mov     cx,x2
  59.       sub     cx,x1                         { Calculate # of bytes to draw }
  60.       cmp     cx,0
  61.       jle     @No_Draw                      { Quit if no pixels to draw }
  62.    @Draw_Loop:
  63.       mov     al,[byte ptr es:di]           { Grab background color }
  64.       add     al,Color                      { Add color }
  65.       stosb                                 { And store the byte }
  66.       loop    @Draw_Loop
  67.    @No_Draw:
  68.  
  69.  There you go! Easy, eh? First check to see if pixels have to be drawn and
  70.  if so, get the backgroundcolor, add the polygon color and store the byte!
  71.  Do this for all pixels in the horizontal line. That's all! The source code
  72.  is simply the 3d system used in the previous trainer so if you got that one
  73.  already, this should be a breeze.
  74.  
  75.  - FLATSHADING -
  76.  
  77.  Ok, next step in 3d is of course shading of the objects. There are many
  78.  different ways of shading, some of which are very complicated and difficult
  79.  to understand. We will start with the most simple and basic shading method
  80.  around and that's flatshading (also called Lambart shading). What's that?
  81.  
  82.  Imagize a cube rotating in front of the sun. When a face turns towards the
  83.  sun, it will get a brighter color. And when the face turns away from the sun,
  84.  it will get a darker color. Important: the entire face will get another color
  85.  using this method. This is called flatshading and is of course more realistic
  86.  than just having faces with one constant color. The light is coming from the
  87.  viewers side in this example (and in most demos anyway).
  88.  
  89.  How do we code this stuff... Well, there are different ways of implementing
  90.  this effect and I'm going to show the easiest one. We simply use the average
  91.  z-value of the polygon to determine the color of this polygon. If you read
  92.  part 10 of these series, you know we need to determine the drawing order of
  93.  the polygons. This is done by adding the z-values of the 4 3d points of the
  94.  polygons. The resulting values are then sorted and the object is drawn from
  95.  back to front. For a complete explanation, go and read part 10. Anyway, the
  96.  z-values we sorted can also be used to determine the color of the polygons.
  97.  Again, the values in the list are the 4 z-values of each polygon added. The
  98.  resulting values are then sorted from small to large. Now, while drawing,
  99.  grab the z-value of the poly (the absolute z-value) from the list and divide
  100.  this by a certain number to get it into the color range you want (I divided
  101.  the value by 8). This is the color of the face. So, for each frame:
  102.  
  103.     - Rotate polygons
  104.     - Store added z-values
  105.     - Sort polygons on these z-values
  106.     - While drawing:
  107.       - Get absolute z-value from list
  108.       - Divide it by color factor
  109.       - Draw the polygon with resulting color
  110.  
  111.  For example: if I reserved colors 0 - 18 for the shading and the z-value can
  112.  be 280 or less (the 4 z-values of the polygon added), the dividing factor
  113.  should be around 16. Because 280/16=17.5 and that fits into the color range
  114.  we wanted. Before this, you should have setup a nice palette for the shading.
  115.  In the source I used a gray-scale palette. And of course, the more colors you
  116.  use, the more realistic the shading will be.
  117.  
  118.  This method is very easy to implement but it's also the most unrealistic one.
  119.  It's good enough for cubes and other simple objects but for more complicated
  120.  objects you'll need a better method of calculating the color of the polygon.
  121.  Flatshading can also be realised by using face-normals and other complicated
  122.  vector calculations. This method will result in better shading. Hmm, yeah, so
  123.  you would like to know about that too, rite? Be prepared for future trainers
  124.  from OT including textfiles on improved flatshading, gouraud, texture, phong,
  125.  z-buffering and a lot more!
  126.  
  127.  For questions, chatting or for suggestions for future trainers, mail me.
  128.  Ya know where to find me... I'm always in for a chat so don't hesitate to
  129.  contact me (or any other member of OT).
  130.  
  131.  That's all for now. Happy coding!
  132.  
  133.      -Vulture/Outlaw Triad-
  134.  
  135. ───────────────────────────────■ Distro Sites ■───────────────────────────────
  136.  
  137.  Call our distribution sites! All our releases are available at:
  138.  
  139.   BlueNose      World HQ          +31 (0)345-619401
  140.   The Force     Distrosite        +31 (0)36-5346967
  141.   Bugs'R'Us     Distrosite        +31 (0)252-686092    More distros wanted!
  142.   The 7 Angels  Distrosite        +31 (0)715-148377    (preferably outside
  143.   ShockWave     South African HQ  +27 (011)888-6345     of the Netherlands)
  144.   Society HQ    United States HQ  +1  (518)465-6721
  145.   ACe World     Brazilian HQ      +55 (21)-259-8847
  146.   Corps Elite   Canadian HQ      +403 (ITS)-PRIVATE
  147.  
  148.  Also check the major FTP/WWW sites for Outlaw Triad productions.
  149.  
  150. ──────────────────────────────────■ Contact ■─────────────────────────────────
  151.  
  152.  Want to contact Outlaw Triad for some reason? You can reach us at our
  153.  distrosites in Holland. Or if you have e-mail access, mail us:
  154.  
  155.    Vulture   (coder/pr)   comma400@tem.nhl.nl
  156.  
  157.  Our internet homepage:
  158.  
  159.    http://www.tem.nhl.nl/~comma400/vulture.html
  160.  
  161.  These internet adresses should be valid at least till june 1997.
  162.  
  163. ──────────────────────────────────────────────────────────────────────────────
  164.  
  165.       Quote: Toe = part of the foot used to find furniture in the dark